import Router from "vue-router";
import Vue from "vue";
import store from "store/index";
import { scrollBar } from 'util/index';

Vue.use(Router);

const Main = () => import("layout/Main.vue");
const EditBlog = () => import("pages/EditBlog.vue");
const Login = () => import("pages/Login.vue");

const Home = () => import("components/Main/Home.vue");
const Archive = () => import("components/Main/Archive.vue");
const Talk = () => import("components/Main/Talk.vue");
const About = () => import("components/Main/About.vue");
const Friends = () => import("components/Main/Friends.vue");
const Details = () => import("components/Main/Details.vue");

const router = new Router({
    mode: "history",
    routes: [
        {
            path: '/',
            redirect: '/home'
        },
        {
            path: "/main",
            component: Main,
            children: [
                {
                    // 首页
                    path: "/home",
                    name: "Home",
                    meta: {
                        note: 'Keep on going never give up'
                    },
                    component: Home,
                },
                {
                    // 目录地图
                    path: "/archive",
                    name: "Archive",
                    meta: {
                        note: 'Here is the information you need'
                    },
                    component: Archive,
                },
                {
                    // 留言区
                    path: "/talk",
                    name: "Talk",
                    meta: {
                        note: 'Technical exchange area'
                    },
                    component: Talk,
                },
                {
                    // 关于我
                    path: "/about",
                    name: "About",
                    meta: {
                        note: 'All things in their being are good for something'
                    },
                    component: About,
                },
                {
                    // 友情链接
                    path: "/friends",
                    name: "Friends",
                    meta: {
                        note: "Hang in there and you'll get something"
                    },
                    component: Friends,
                },
                {
                    // 文章详情页
                    path: "/details/:id",
                    name: "Details",
                    component: Details,
                },
            ],
        },
        {
            path: "/editBlog",
            component: EditBlog,
            name: "editBlog",
        },
        {
            path: "/login",
            component: Login,
            name: "login",
        },
    ]
});

router.beforeEach((to, from, next) => {
    const {meta, name} = to;
    meta.note && store.commit("siteInfo/modifyTitle", {
        title: name,
        note: meta.note
    });
    if (to.name == "editBlog") {
        if (store.state.loginUser.loginUser) {
            next();
        } else {
            next("/login");
        }
    } else if (to.fullPath == "/" || to.fullPath == "/main") {
        next("/home");
    } else {
        next();
    }
    // 跳转路由后滚动到页面顶部
    scrollBar(0, 0);
});

export default router;
